namespace TripleA.Core.Database.SqlServerCe
{
    using System.Data;
    using TripleA.Core.Database;

    /// <summary>
    /// Fluent wrapper to core data access components to execute an ad-hoc (inline) sql statement
    /// </summary>
    public class SqlServerCeAdhocCommand : AdhocCommandBase
    {
        /// <summary>
        /// This attempts 
        /// </summary>
        /// <returns></returns>
        public static SqlServerCeAdhocCommand UsingSmartConnection(string connection)
        {
            return (ConfigurationManager.ConnectionStrings[connection] != null)
                       ? UsingConnectionNamed(connection)
                       : UsingConnectionString(connection);
        }

        /// <summary>
        /// Main entry point to fluent interface. Creates a new instance of 
        /// an Adhoc database operation with the connection string name specified
        /// </summary>
        /// <returns></returns>
        public static SqlServerCeAdhocCommand UsingConnectionNamed(string connectionName)
        {
            return UsingConnectionString(ConfigurationManager.ConnectionStrings[connectionName].ConnectionString);
        }

        /// <summary>
        /// Main entry point to fluent interface. Creates a new instance of 
        /// an Adhoc database operation with the connection string specified
        /// </summary>
        /// <returns></returns>
        public static SqlServerCeAdhocCommand UsingConnectionString(string connectionString)
        {
            return new SqlServerCeAdhocCommand
                       {
                           _connectionString = connectionString
                       };
        }

        protected override IDbConnection NewConnection()
        {
            return new SqlCeConnection();
        }
    }
}